#include "opinfcontroller.h"
#include <QTextCodec>

using namespace stefanfrings;

OpinfController::OpinfController(QObject* parent)
    : HttpRequestHandler(parent) {
    // empty
}

void OpinfController::service(HttpRequest &request, HttpResponse &response)
{
    //接收病人手术信息
    response.setHeader("Access-Control-Allow-Origin", "https://slides.thearay.net");
    response.write("ok",true);
    QByteArray buf;
    buf=request.getMethod();
    qDebug()<<__LINE__<<"getMethod="<<buf;//POST GET 等访问方式
    buf=request.getPath();
    qDebug()<<__LINE__<<"getPath="<<buf;//http访问时追加路径
    buf=request.getVersion();
    qDebug()<<__LINE__<<"getVersion="<<buf;//HTTP版本("HTTP/1.1")
    buf=request.getBody();
    qDebug()<<__LINE__<<"getBody="<<buf;//传输过来的数据

    QByteArray urlDecode = QByteArray::fromPercentEncoding(buf);//URL解码
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//指定QString的编码方式
    QString str = codec->toUnicode(urlDecode);//nameArray可以是char*，可以是QByteArray
    qDebug()<<__LINE__<<"接收到数据:"<<str;

    emit sendOpinfMain("1",str);
}

void OpinfController::overLight(HttpRequest &request, HttpResponse &response)
{
    //"{\"over_light\":1}"
    response.setHeader("Access-Control-Allow-Origin", "https://slides.thearay.net");
    response.write("ok",true);
    QByteArray buf;
    buf=request.getMethod();
    qDebug()<<__LINE__<<"getMethod="<<buf;//POST GET 等访问方式
    buf=request.getPath();
    qDebug()<<__LINE__<<"getPath="<<buf;//http访问时追加路径
    buf=request.getVersion();
    qDebug()<<__LINE__<<"getVersion="<<buf;//HTTP版本("HTTP/1.1")
    buf=request.getBody();
    qDebug()<<__LINE__<<"getBody="<<buf;//传输过来的数据

    QByteArray urlDecode = QByteArray::fromPercentEncoding(buf);//URL解码
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//指定QString的编码方式
    QString str = codec->toUnicode(urlDecode);//nameArray可以是char*，可以是QByteArray
    qDebug()<<__LINE__<<"接收到数据:"<<str;

    emit sendOpinfMain("2",str);
}

void OpinfController::bottleState(HttpRequest &request, HttpResponse &response)
{
    //"[{\"drugNum\":\"2\",\"value\":\"镇静剂(10ml)\"},{\"drugNum\":\"5\",\"value\":\"醋托啡(10ml)\"}]"
    response.setHeader("Access-Control-Allow-Origin", "https://slides.thearay.net");
    response.write("ok",true);
    QByteArray buf;
    buf=request.getMethod();
    qDebug()<<__LINE__<<"getMethod="<<buf;//POST GET 等访问方式
    buf=request.getPath();
    qDebug()<<__LINE__<<"getPath="<<buf;//http访问时追加路径
    buf=request.getVersion();
    qDebug()<<__LINE__<<"getVersion="<<buf;//HTTP版本("HTTP/1.1")
    buf=request.getBody();
    qDebug()<<__LINE__<<"getBody="<<buf;//传输过来的数据

    QByteArray urlDecode = QByteArray::fromPercentEncoding(buf);//URL解码
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//指定QString的编码方式
    QString str = codec->toUnicode(urlDecode);//nameArray可以是char*，可以是QByteArray
    qDebug()<<__LINE__<<"接收到数据:"<<str;

    emit sendOpinfMain("3",str);
}
